From faf02c345ec0b2a5bbd45b4f94ffd59e75a42435 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Edwin=20T=C3=B6r=C3=B6k?= Date: Fri, 8 Jan 2021 11:57:37 +0000 Subject: [PATCH] tools/oxenstored: Backport find_opt/update from 4.06 MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit We are currently on OCaml 4.02 as minimum version. To make the followup optimizations compile backport these functions from OCaml 4.06. This implementation is less efficient than the one in the 4.06 standard library which has access to the internals of the Map. Signed-off-by: Edwin Török Acked-by: Christian Lindig --- tools/ocaml/xenstored/stdext.ml | 19 +++++++++++++++++++ tools/ocaml/xenstored/trie.ml | 2 ++ 2 files changed, 21 insertions(+) diff --git a/tools/ocaml/xenstored/stdext.ml b/tools/ocaml/xenstored/stdext.ml index e1567c4dfa..116920917a 100644 --- a/tools/ocaml/xenstored/stdext.ml +++ b/tools/ocaml/xenstored/stdext.ml @@ -50,6 +50,25 @@ module Filename = struct cmd :: args |> List.map quote |> String.concat " " end +module Map = struct + module Make(Ord: Map.OrderedType) = struct + + include Map.Make(Ord) + + let find_opt k t = try Some (find k t) with Not_found -> None + + let update k f t = + let r = find_opt k t in + let r' = f r in + match r, r' with + | None, None -> t + | Some _, None -> remove k t + | Some r, Some r' when r == r' -> t + | _, Some r' -> add k r' t + + end +end + module String = struct include String let of_char c = String.make 1 c diff --git a/tools/ocaml/xenstored/trie.ml b/tools/ocaml/xenstored/trie.ml index dc42535092..f513f4e608 100644 --- a/tools/ocaml/xenstored/trie.ml +++ b/tools/ocaml/xenstored/trie.ml @@ -13,6 +13,8 @@ * GNU Lesser General Public License for more details. *) +open Stdext + module Node = struct type ('a,'b) t = { -- 2.30.2